<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('work_injury_records', function (Blueprint $table) {
            $table->id();
            $table->timestamps();

            // 员工信息
            $table->unsignedBigInteger('employee_id');
            $table->string('employee_name', 100)->nullable();
            $table->string('employee_number', 50)->nullable();
            $table->string('id_card_number', 18)->nullable();

            // 工伤基本信息
            $table->string('injury_number', 50)->unique(); // 工伤编号
            $table->datetime('injury_time'); // 工伤时间
            $table->string('injury_location', 200); // 工伤地点
            $table->string('injury_type', 50); // 工伤类型：工作时间、上下班途中等
            $table->text('injury_description'); // 工伤经过描述
            $table->text('injury_cause'); // 工伤原因

            // 受伤情况
            $table->string('injury_level', 20); // 伤情等级：轻伤、一般伤、重伤
            $table->json('injured_parts'); // 受伤部位
            $table->boolean('is_disabled')->default(false); // 是否致残
            $table->string('disability_level', 20)->nullable(); // 伤残等级
            $table->boolean('is_fatal')->default(false); // 是否工亡

            // 报告和认定
            $table->datetime('report_time')->nullable(); // 报告时间
            $table->string('report_person', 100)->nullable(); // 报告人
            $table->datetime('confirmation_time')->nullable(); // 认定时间
            $table->string('confirmation_result', 50)->nullable(); // 认定结果
            $table->string('confirmation_number', 100)->nullable(); // 认定文号

            // 责任认定
            $table->string('responsibility_type', 50)->nullable(); // 责任类型：全部责任、部分责任、无责任
            $table->decimal('responsibility_percentage', 5, 2)->nullable(); // 责任比例

            // 工作信息
            $table->unsignedBigInteger('department_id')->nullable();
            $table->string('department_name', 100)->nullable();
            $table->unsignedBigInteger('position_id')->nullable();
            $table->string('position_name', 100)->nullable();
            $table->decimal('monthly_salary', 10, 2)->nullable(); // 月工资

            // 治疗医院
            $table->string('hospital_name', 200)->nullable();
            $table->string('hospital_level', 50)->nullable(); // 医院等级
            $table->string('treatment_type', 50)->nullable(); // 治疗类型：门诊、住院

            // 保险信息
            $table->boolean('has_work_injury_insurance')->default(false);
            $table->string('insurance_company', 100)->nullable();
            $table->string('insurance_policy_number', 100)->nullable();

            // 状态管理
            $table->string('status', 20)->default('reported'); // reported, confirmed, treating, recovered, closed
            $table->boolean('is_closed')->default(false);
            $table->datetime('closed_at')->nullable();
            $table->text('closing_reason')->nullable();

            // 统计信息
            $table->decimal('total_medical_cost', 12, 2)->default(0); // 总医疗费用
            $table->decimal('total_compensation', 12, 2)->default(0); // 总补偿费用
            $table->integer('treatment_days')->default(0); // 治疗天数
            $table->integer('leave_days')->default(0); // 工伤假天数

            // 备注和附件
            $table->text('notes')->nullable();
            $table->json('attachments')->nullable();

            // 创建者信息
            $table->string('created_by', 100)->nullable();
            $table->string('updated_by', 100)->nullable();

            // 索引
            $table->index(['employee_id', 'injury_time']);
            $table->index(['injury_time']);
            $table->index(['status']);
            $table->index(['is_disabled']);
            $table->index(['is_fatal']);
            $table->index(['department_id']);
            $table->index(['injury_number']);
            $table->index(['is_closed']);

            // 外键约束
            $table->foreign('employee_id')->references('id')->on('employees')->onDelete('cascade');
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
            $table->foreign('position_id')->references('id')->on('positions')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('work_injury_records');
    }
};
